﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MvcDemo.FilterTest;
using MvcDemo.Models;

namespace MvcDemo.Controllers
{
    public class FilterTestController : Controller
    {
        private MvcDemoContext db = new MvcDemoContext();

        // GET: FilterTest
        [MyActionFilter]
        [MyCustomerFilter]
        public ActionResult Index()
        {
            var user = new User().GetData();

            return View(user);
        }

        // GET: FilterTest/Details/5
        [TestAuthorize]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Area area = new Area { Id = 1, Name = "郑州", Description = "屌丝之城", PointX = 100, PointY = 100 };
            if (area == null)
            {
                return HttpNotFound();
            }
            return View(area);
        }
        /// <summary>
        /// 错误演示
        /// </summary>
        /// <returns></returns>
        [TestHandleError]
        public ActionResult GetErr()
        {
            int a = 0;
            int b = 1 / a;
            return View();
        }

        public ActionResult Error(string errStr)
        {
            return View(errStr);
        }

        public ActionResult Login()
        {
            return View();
        }

        // GET: FilterTest/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: FilterTest/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Description,PointX,PointY")] Area area)
        {
            if (ModelState.IsValid)
            {
                db.Areas.Add(area);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(area);
        }

        // GET: FilterTest/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Area area = db.Areas.Find(id);
            if (area == null)
            {
                return HttpNotFound();
            }
            return View(area);
        }

        // POST: FilterTest/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Description,PointX,PointY")] Area area)
        {
            if (ModelState.IsValid)
            {
                db.Entry(area).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(area);
        }

        // GET: FilterTest/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Area area = db.Areas.Find(id);
            if (area == null)
            {
                return HttpNotFound();
            }
            return View(area);
        }

        // POST: FilterTest/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Area area = db.Areas.Find(id);
            db.Areas.Remove(area);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

 
    }
}
